package LC;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Classname LC655
 * @Description // https://leetcode-cn.com/problems/print-binary-tree/ 树
 * @Date 2021/4/21 16:45
 * @Created by yin
 */

// dfs 中序遍历，记录遍历序号、层、值
public class LC655 {
    private int index;
    ArrayList<int[]> arrayList = new ArrayList<>();
    int[] temp = new int[3];

    public List<List<String>> printTree(TreeNode root) {
        List<List<String>> ans = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            ArrayList<String> strings = new ArrayList<>(index);
            for (int p = 0; p < index; p++) {
                strings.add("");
            }
            ans.add(strings);
        }
        for (int i = 0; i < arrayList.size(); i++) {
            int[] ints = arrayList.get(i);
            int val = ints[0];
            int id = ints[i];
            int level = ints[2];
            List<String> strings = ans.get(level);
            Collections.replaceAll(strings,strings.get(id),String.valueOf(val));
        }
        return ans;
    }

    public void dfs(TreeNode node, int level) {
        if (node == null) return;
        dfs(node.left, level + 1);
        temp[0] = node.val;
        temp[1] = index++;
        temp[2] = level;
        arrayList.add(temp);
        dfs(node.right, level + 1);
    }

    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>(3);
        Collections.replaceAll(strings,strings.get(1),"s");
        System.out.println(strings);
    }
}
